# -*- coding: utf-8 -*-
import numpy as np

class StandardScaler( object ):
    
    def __init__( self ):
        self.mean_ = None
        self.scale_ = None
    
    def fit( self, X ):
        '''
        根据X，获得训练数据集的均值和方差
        '''
        self.mean_ = np.array( [ np.mean( X[:, i] ) for i in range( X.shape[1] ) ] )
        self.scale_ = np.array( [ np.std( X[:, i] ) for i in range( X.shape[1] ) ] )
        return self
    
    def transform( self, X ):
        '''
        根据已获得的mean_和scale_进行归一化处理
        '''
        # 设置一个空的矩阵
        resX = np.empty( X.shape, dtype = float )
        for col in range( X.shape[1] ):
            resX[:, col] = ( X[:, col] - self.mean_[:, col] ) / self.scale_[:, col]
            
        return resX
        